//function for document ready event
$(function() {
  	// auto fill search keyword with selected text
		function autoFillSelText(){
			var sel;
				if (document.selection) {
					sel = document.selection.createRange().text;
					sel = sel.replace(/(^\s+)|(\s+$)/ig,"");
				}else if (window.getSelection) {
				  var selection = window.getSelection();
					if (selection.rangeCount > 0) {
						sel = selection.getRangeAt(0).toString(); 
						sel = sel.replace(/(\s{2,})/ig," ");
						sel = sel.replace(/(^\s+)|(\s+$)/ig,"");
					}	
				}
				
				if(sel)	{
					$('#txtSearchKeyword').val(sel).focus();
				}	
		}		
		
		$("*").mouseup(autoFillSelText);  
		
		$(".searchButton").click(function(){
			var searchButtonName =	$(this).attr("name");
	    var elForm = $("FORM[@name='searchBoxForm']")[0];
	    var keywordField=$('#txtSearchKeyword')[0];
			if(searchButtonName == "btnSearchGoogle"){
				//alert("go to btnSearchGoogle");
				
				elForm.setAttribute("id",'searchbox_014743101348296413592:3z-wki4rrxk');
				elForm.method="get";
				elForm.setAttribute("action","/pnb/searchGoogle.jsp");
				var elInput = document.createElement('INPUT');
				elInput.setAttribute('type', 'hidden');
				elInput.setAttribute('name', 'cx');
				elInput.setAttribute('value', '014743101348296413592:3z-wki4rrxk');
				elForm.appendChild(elInput);

				var elInput1 = document.createElement('INPUT');
				elInput1.setAttribute('type', 'hidden');
				elInput1.setAttribute('name', 'cof');
				elInput1.setAttribute('value', 'FORID:11');
				elForm.appendChild(elInput1);

				var elInput2 = document.createElement('INPUT');
				elInput2.setAttribute('type', 'hidden');
				elInput2.setAttribute('name', 'ie');
				elInput2.setAttribute('value', 'gb2312');
				elForm.appendChild(elInput2);
              
				if(keywordField) {
						keywordField.setAttribute('name', 'q');
				}

				var eleInputs = elForm.getElementsByTagName('INPUT');
				for(j = 0; j < eleInputs.length; j++){
				   var elName = eleInputs[j].getAttribute('name');
					 if(elName){
						 if (elName != 'cx' && elName != 'cof' && elName != 'q' && elName != 'ie'){
							elForm.removeChild(eleInputs[j]);    
						 }
					 }
				}
				
				elForm.submit();
			} else if (searchButtonName == "btnSearchBaidu" ){
			//do search baidu
			}else if (searchButtonName == "btnSearchYahoo" ){
			}else if (searchButtonName == "btnSearchSohu" ){
			}else if (searchButtonName == "btnSearchSogou" ){
			}else if (searchButtonName == "btnSearchTaobao" ){
			}else if (searchButtonName == "btnSearchPaipai" ){
			}else if (searchButtonName == "btnSearchEachnet" ){
			}
		});
});

